// 活跃度系统配置
const ACTIVITY_CONFIG = {
  MAX_DAILY_POINTS: 666,
  ACTIONS: {
    CHAT: {
      points: 4,
      limit: "perMinute",
      description: "聊天室发言",
    },
    ARTICLE: {
      points: 300,
      limit: "perDay",
      description: "发表文章",
    },
    COMMENT: {
      points: 30,
      limit: "perDay",
      maxCount: 5,
      description: "评论文章",
    },
    VIEW: {
      points: 5,
      limit: "perDay",
      maxCount: 20,
      description: "查看文章",
    },
    REWARD: {
      points: 15,
      limit: "none",
      description: "打赏文章",
    },
    THANK: {
      points: 15,
      limit: "none",
      description: "感谢文章",
    },
    THANK_COMMENT: {
      points: 10,
      limit: "none",
      description: "感谢评论",
    },
    VOTE: {
      points: 20,
      limit: "perDay",
      maxCount: 2,
      description: "支持/反对文章",
    },
    ADOPT: {
      points: 50,
      limit: "perDay",
      description: "采纳答案",
    },
  },
};

// 本地存储的键名
const STORAGE_KEY = "activity_manager_data";

class ActivityManager {
  constructor() {
    this.loadStateFromStorage();
  }

  // 从本地存储加载状态
  loadStateFromStorage() {
    try {
      const savedData = uni.getStorageSync(STORAGE_KEY);
      if (savedData) {
        const parsedData = JSON.parse(savedData);
        this.dailyPoints = parsedData.dailyPoints || 0;
        this.actionCounts = parsedData.actionCounts || {};
        this.lastActionTime = parsedData.lastActionTime || {};

        // 检查是否需要重置每日数据
        this.checkDayReset();
      } else {
        this.initializeState();
      }
    } catch (e) {
      console.error("加载活跃度数据失败:", e);
      this.initializeState();
    }
  }

  // 初始化状态
  initializeState() {
    this.dailyPoints = 0;
    this.actionCounts = {};
    this.lastActionTime = {};
    this.lastResetDay = this.getCurrentDay();
  }

  // 保存状态到本地存储
  saveStateToStorage() {
    try {
      const data = {
        dailyPoints: this.dailyPoints,
        actionCounts: this.actionCounts,
        lastActionTime: this.lastActionTime,
        lastResetDay: this.lastResetDay,
      };
      uni.setStorageSync(STORAGE_KEY, JSON.stringify(data));
    } catch (e) {
      console.error("保存活跃度数据失败:", e);
    }
  }

  // 获取当前日期（用于检查日期变更）
  getCurrentDay() {
    const now = new Date();
    return `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
  }

  // 检查是否需要重置每日数据
  checkDayReset() {
    const currentDay = this.getCurrentDay();
    if (!this.lastResetDay || this.lastResetDay !== currentDay) {
      this.resetDaily();
      this.lastResetDay = currentDay;
    }
  }

  // 检查操作是否可以获得积分
  canGetPoints(actionType) {
    const action = ACTIVITY_CONFIG.ACTIONS[actionType];
    if (!action) return false;

    // 检查每日积分上限
    if (this.dailyPoints >= ACTIVITY_CONFIG.MAX_DAILY_POINTS) {
      return false;
    }

    // 检查操作限制
    switch (action.limit) {
      case "perMinute":
        const lastTime = this.lastActionTime[actionType] || 0;
        if (Date.now() - lastTime < 60000) {
          return false;
        }
        break;
      case "perDay":
        const count = this.actionCounts[actionType] || 0;
        if (action.maxCount && count >= action.maxCount) {
          return false;
        }
        break;
    }

    return true;
  }

  // 记录操作并获得积分
  recordAction(actionType) {
    if (!this.canGetPoints(actionType)) {
      return {
        success: false,
        points: 0,
        description: ACTIVITY_CONFIG.ACTIONS[actionType].description,
      };
    }

    const action = ACTIVITY_CONFIG.ACTIONS[actionType];
    this.dailyPoints += action.points;
    this.lastActionTime[actionType] = Date.now();
    this.actionCounts[actionType] = (this.actionCounts[actionType] || 0) + 1;

    // 保存状态到本地存储
    this.saveStateToStorage();

    return {
      success: true,
      points: action.points,
      description: action.description,
    };
  }

  // 获取当前活跃度百分比
  getActivityPercentage() {
    return (
      (this.dailyPoints / ACTIVITY_CONFIG.MAX_DAILY_POINTS) *
      100
    ).toFixed(1);
  }

  // 重置每日数据
  resetDaily() {
    this.dailyPoints = 0;
    this.actionCounts = {};
    this.lastActionTime = {};
    this.saveStateToStorage();
  }
}

export const activityManager = new ActivityManager();
export default activityManager;
